package com.suncnpap.library.controller;

import com.suncnpap.library.model.ChineseClassification;
import com.suncnpap.library.service.ChineseClassificationService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.app.util.common.HandlerResult;
import com.app.util.common.ResponseUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.datamanage.common.DataImpUtil;

/** 
 * 说明：中图分类Controller
 */
@Controller
@RequestMapping(value="/library/chineseClassification")
public class ChineseClassificationController extends BaseController {
	
	//private static final Logger log = Logger.getLogger(ChineseClassificationController.class);
	
	@Autowired
	DataImpUtil dataImpUtil;
	
	@Autowired
	ChineseClassificationService chineseClassificationService;
	
	
	/**
	 * 列表页面数据处理，包括查询
	 * @param
	 * @param response
	 */
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{


//			 result =new HandlerResult();
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {

			Page page = this.setPage();
			
//			int count = chineseClassificationService.findCount(page);
//			if(count == 0){
//				initData();
//			}
			
			String strParentNodeId = this.getParameter("strParentNodeId", "");
			if(!strParentNodeId.equals("")){
				page.addQueryAnd("strParentNodeId", strParentNodeId);
			}
			
			page.setSort("strCode");
			page.setOrder("asc");
			
			page = chineseClassificationService.list(page);
			List<ChineseClassification> tempList = page.getDataList();
			
			List<Map<String,Object>> childrenList = new ArrayList<Map<String,Object>>();
			
			if(tempList!=null && tempList.size()>0){
				for(ChineseClassification obj:tempList){
						Map<String,Object> temp = new HashMap<String,Object>();
						temp.put("strNodeId", obj.getStrId());
						temp.put("strNodeName", obj.getStrNodeName());
						temp.put("expand", false);
						childrenList.add(temp);
				}
				
			}
			result.put("objList", childrenList);
			
		}
		
		ResponseUtil.writeJson(result, response);

	}

	@RequestMapping(value="/getlist")
	public void getList(HttpServletRequest request, HttpServletResponse response) throws Exception{


			 HandlerResult result =new HandlerResult();
//		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {

			Page page = this.setPage();

//			int count = chineseClassificationService.findCount(page);
//			if(count == 0){
//				initData();
//			}

			String strParentNodeId = this.getParameter("strParentNodeId", "");
			if(!strParentNodeId.equals("")){
				page.addQueryAnd("strParentNodeId", strParentNodeId);
			}

			page.setSort("strCode");
			page.setOrder("asc");

			page = chineseClassificationService.list(page);
			List<ChineseClassification> tempList = page.getDataList();

			List<Map<String,Object>> childrenList = new ArrayList<Map<String,Object>>();

			if(tempList!=null && tempList.size()>0){
				for(ChineseClassification obj:tempList){
					Map<String,Object> temp = new HashMap<String,Object>();
					temp.put("strNodeId", obj.getStrId());
					temp.put("strNodeName", obj.getStrNodeName());
					temp.put("expand", false);
					childrenList.add(temp);
				}

			}
			result.put("objList", childrenList);

		}

		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody ChineseClassification obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				init(obj);
				chineseClassificationService.add(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody ChineseClassification obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				chineseClassificationService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					chineseClassificationService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				ChineseClassification obj = chineseClassificationService.findById(strId);
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/imp")
	public void imp(HttpServletRequest request, HttpServletResponse response) throws Exception{
//		initData();
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				initData();
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	private List<Map<String,Object>> creatJsonTree(List<ChineseClassification> menuList){
		List<Map<String,Object>> tempList = new ArrayList<Map<String,Object>>();
		if(menuList != null){
			for(ChineseClassification obj: menuList){
				Map<String,Object> tempMap = new HashMap<String,Object>();
				
				tempMap.put("strParentNodeId", obj.getStrId());
				tempMap.put("strParentNodeName", obj.getStrNodeName());
				List<Map<String,Object>> tempJson = creatJsonTree(obj.getChildren());
				if(tempJson!=null && tempJson.size()>0){
					tempMap.put("children", tempJson);
				}
				tempMap.put("expand", false);
				tempList.add(tempMap);
			}
		}
		return tempList;
	}
	
	private List<ChineseClassification> creatChineseClassificationTree(List<ChineseClassification> menuList,Map<String, List<ChineseClassification>> menuListMap){
		List<ChineseClassification> list=null;
		menuListMap = menuListMap==null?menuListMap:new HashMap<String, List<ChineseClassification>>();
		for(int i=0;menuList!=null&&i<menuList.size();i++){
			ChineseClassification obj=menuList.get(i);
			System.out.println(obj.getStrNodeName());
			list=list==null?new ArrayList<ChineseClassification>():list;
			if(obj!=null){
				obj.setChildren(creatChineseClassificationTree(menuListMap.get(obj.getStrId()),menuListMap));
				list.add(obj);
			}
			
			
		}
		return list;
	}
	
	public void initData(){
		
		String strCreatId = GlobaUser.getStrId(getRequest().getHeader("token"));//创建人ID
		String strCreatName = GlobaUser.getStrCname(getRequest().getHeader("token"));//创建人姓名
		String strSchoolId = GlobaUser.getStrSchoolId(getRequest().getHeader("token"));//独立机构ID
		
//		DataImpUtil impUtil = new DataImpUtil();
		dataImpUtil.init();
		List<ChineseClassification> list = dataImpUtil.initTableData(strCreatId,strCreatName,strSchoolId);
		
		chineseClassificationService.deleteBySchoolId(strSchoolId);
		chineseClassificationService.addList(list);
	}
	
}
